package com.jeesite.modules.reject.web;

import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.reject.entity.RejectHeader;
import com.jeesite.modules.reject.entity.RejectLines;
import com.jeesite.modules.reject.service.RejectHeaderService;

/**
 * 拒收单Controller
 * @author 码脑洞
 * @version 2025-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/reject/header")
public class RejectHeaderController extends BaseController {

	@Autowired
	private RejectHeaderService rejectHeaderService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public RejectHeader get(Long autoId, boolean isNewRecord) {
		return rejectHeaderService.get(autoId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("reject:header:view")
	@RequestMapping(value = {"list", ""})
	public String list(RejectHeader rejectHeader, Model model) {
		model.addAttribute("rejectHeader", rejectHeader);
		return "modules/reject/rejectHeaderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("reject:header:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<RejectHeader> listData(RejectHeader rejectHeader, HttpServletRequest request, HttpServletResponse response) {
		rejectHeader.setPage(new Page<>(request, response));
		Page<RejectHeader> page = rejectHeaderService.findPage(rejectHeader);
		return page;
	}
	
	/**
	 * 查询子表数据
	 */
	@RequiresPermissions("reject:header:view")
	@RequestMapping(value = "linesListData")
	@ResponseBody
	public Page<RejectLines> subListData(RejectLines rejectLines, HttpServletRequest request, HttpServletResponse response) {
		rejectLines.setPage(new Page<>(request, response));
		Page<RejectLines> page = rejectHeaderService.findSubPage(rejectLines);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("reject:header:view")
	@RequestMapping(value = "form")
	public String form(RejectHeader rejectHeader, Model model) {
		model.addAttribute("rejectHeader", rejectHeader);
		return "modules/reject/rejectHeaderForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("reject:header:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated RejectHeader rejectHeader) {
		rejectHeaderService.save(rejectHeader);
		return renderResult(Global.TRUE, text("保存拒收单成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("reject:header:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(RejectHeader rejectHeader) {
		rejectHeaderService.delete(rejectHeader);
		return renderResult(Global.TRUE, text("删除拒收单成功！"));
	}
	
	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("reject:header:view")
	@RequestMapping(value = "rejectHeaderSelect")
	public String rejectHeaderSelect(RejectHeader rejectHeader, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("rejectHeader", rejectHeader);
		return "modules/reject/rejectHeaderSelect";
	}
	
}